//把请求封装

const http = {}

//不变因素:获取token、请求头、请求基础路径、当403用户未登录处理error
//变的因素：type方式、请求路径、success回调函数、
http.ajax = function ({ type, url, data, success }) {
    //基础路径
    const BASE_URL = 'http://localhost:8080/api/v1';
    //获取token
    const token = window.localStorage.getItem('token');

    const requestOption = {
        headers: { Authorization: token },
        type: type,
        url: BASE_URL + url,
        data: data,
        success: success,
        error: function (jqXHR) {
            if (jqXHR.status === 403) {
                parent.window.location.href = './login.html'
            }
        }
    };
    const isFormData = requestOption.data instanceof FormData;
    // console.log(isFormData); //true
    //判断data，如果是FormData，设置contentType:False,processData：false
    if (isFormData) {
        requestOption.contentType = false;
        requestOption.processData = false;
    }
    $.ajax(requestOption);
}